package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.VideoSafeRecordDao;
import cn.topevery.garden.entity.po.VideoSafeRecord;
import cn.topevery.garden.entity.query.VideoSafeRecordQuery;
import cn.topevery.garden.entity.vo.VideoSafeRecordVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.enums.DbTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-09-17
 */
@Repository
public class VideoSafeRecordDaoImpl extends BaseDaoImpl<VideoSafeRecord> implements VideoSafeRecordDao {
    @Override
    public <P extends DbCsp> List<VideoSafeRecord> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<VideoSafeRecordVO> listPage(VideoSafeRecordQuery query) {
        String table = "t_video_safe_record tvsr";

        String columns = "tvsr.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tvsr.c_id desc");
        sqlQueryMate
                .where(true, "tvsr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tvsr.c_device_code", ScriptConditionEnum.Eq, query.getCode())
                .where(StringUtils.isNotBlank(query.getTypeName()), "tvsr.c_type", ScriptConditionEnum.Eq, query.getTypeName())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, VideoSafeRecordVO.class);
    }

    @Override
    public VideoSafeRecord getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void updateDeviceCode(String userId, String oldDeviceCode, String newDeviceCode) {
        this.run("update t_video_safe_record set c_device_code=:newCode,c_db_last_update_date=now(),c_db_last_update_id=:userid where c_device_code=:oldCode", CommandType.Text, (dbCmd) -> {
            dbCmd.addInParameter("oldCode",DbTypeEnum.VARCHAR,oldDeviceCode);
            dbCmd.addInParameter("newCode",DbTypeEnum.VARCHAR,newDeviceCode);
            dbCmd.addInParameter("userid",DbTypeEnum.INTEGER,userId);
            dbCmd.executeNonQuery();
        });
    }
}
